;(function($){
	/*
	 * 需要引用CSS
	 * /static/group/E_coupon/coupon_get2.css
	 */
	$.coupon_got_info = $.coupon_got_info || {}
	$.fn.coupon_got_info = function(opts){
		var el = this;
		var opt = {
			mno : 0,
			dataBack : function(){},
			closeBack : function(){},
			wait : function(){},
			goon : function(){},
		}
		var param = $.extend(opt,opts);
		var app = new App(el,param);
		$.coupon_got_info = $.extend($.coupon_got_info,app);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.coupon_gotDefault();
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
		},
		initCoupon_gotClearHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			el.empty();
		},
		initCoupon_gotHtml : function(ds){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var html = '';
			var enableHtml = '';
			var usedHtml = '';
			var disableHtml = '';
//			console.log(JSON.stringify(ds));
			$(ds).each(function(k,v){
				
				var condition = 1;
				if(v.status_c=="可用"&&v.available>0){
					condition = 1
				}else if(v.status_c=="可用"&&v.available==0){
					condition = 2
				}else{
					condition = 3
				}
//			 	condition = 3;
				if(condition==1){
					enableHtml +=	'<li>'+
	                    				'<div class="coupon-item c-enable -c-nopointer coupon-item-new">'+
											'<div class="coupon-item-relative">'+
		                    					'<div class="c-detail" data-couponid="'+v.couponid+'">'+
		                    						'<div class="c-msg c-dong c-nopointer">'+
			                    						'<div class="c-top-dong"></div>'+
			                    						'<div class="item-selected-cancel hide">'+$.i18n.propJsp("取消勾选")+'</div>'+
														'<div class="item-c-content">'+
															'<div class="c-content">'+
				                    							'<div class="c-price">'+
					                    							'<em>￥'+(v.sellingprice||0)+'</em>'+//sellingprice act_rate
				                    							'</div>'+
				                    							'<div class="c-limit">'+
																	'<span class="custom_tip" data-tip="'+v.cptext+'" style="margin-left:5px;">'+v.cptext+'</span>'+
			                                             		'</div>'+
															'</div>'+
		                                             		'<div class="c-time c-time-dong">'+
//		                                             			'<span>有效期至2020-04-10</span>'+
																'<span>可用['+v.available+']</span>'+
																'<span style="margin-left:5px;">已用['+(v.quantity-v.available)+']</span>'+
		                                         			'</div>'+
														'</div>'+
	                                     			'</div>'+
	                                     			'<div class="c-type c-type-dong">'+
//                                                  	'<span class="c-type-l">[东券]</span>'+
//                                                    	'<span class="c-type-r">可用['+v.available+']</span>'+
//                                                    	'<span class="c-type-l">['+v.cpcode+']</span>'+
														$.i18n.propJsp("期限")+"["+v.edate+"]"+
	                                            	'</div>'+
	                                        	'</div>'+
												'<div class="q-opbtns">'+
	                                        		'<b></b>'+
													'<div class="opbtn">'+
														'<a class="useCoupon" href="javasrcipt:void(0);" data-cpcode="cpcode" data-couponid="'+v.couponid+'">'+
															'<span class="json hide">'+JSON.stringify(v)+'</span>'+
															$.i18n.propJsp("马上使用")+
	//														txt+
														'</a>'+
													'</div>'+
	                               				'</div>'+
	                               				'<div class="top-corner">'+
	                               					'<div class="box"></div>'+
	                               				'</div>'+
	                                			'<div class="bottom-corner">'+
	                                		   		'<div class="box"></div>'+
	                                			'</div>'+
											'</div>'+
                                        	'<div class="c-info">'+
                                        		(v.remark||"")+//$.i18n.propJsp("期限:")+
                                    		'</div>'+
                                		'</div>'+
		                    		'</li>';
				}else if(condition==2){
					usedHtml += 	'<li>'+
										'<div class="coupon-item c-disable coupon-item-new">'+
											'<div class="c-detail-disable">'+
												'<div class="c-msg">'+
													'<div class="c-top"></div>'+
													
													'<div class="item-c-content">'+
														'<div class="c-content">'+
															'<div class="c-price">'+
																'<em>￥'+(v.sellingprice||0)+'</em>'+
															'</div>'+
															'<div class="c-limit">'+
																'<span class="custom_tip" data-tip="'+v.cptext+'" style="margin-left:5px;">'+v.cptext+'</span>'+
															'</div>'+
														'</div>'+
														'<div class="c-time c-time-dong">'+
//															'<span>有效期至2020-04-12</span>'+
															'<span>可用['+v.available+']</span>'+
															'<span style="margin-left:5px;">已用['+(v.quantity-v.available)+']</span>'+
			                                            '</div>'+
													'</div>'+
		                                        '</div>'+
		                                        '<div class="c-type">'+
//		                                        	'<span class="c-type-l">['+v.cpcode+']</span>'+
//			                                        '<span class="c-type-r" id="59430242451">[限品类]</span>'+
//			                                        '<span title="可与店铺东券叠加使用" style="margin-left:7px;">[可叠加]</span>'+
													$.i18n.propJsp("期限")+"["+v.edate+"]"+
		                                        '</div>'+
		                                    '</div>'+
		                                    '<div class="c-info">'+
		                                    	'<i class="c-info-qmark"></i>'+
		                                    	'<span>'+
		                                    		'<div class="virtual-desc" title="'+$.i18n.propJsp("已用完")+'">'+//$.i18n.propJsp("已用完")
//		                                    			'此券在2020-04-11 00:00:00后可使用'+
		                                    			$.i18n.propJsp("已用完")+ 
//														(v.remark||"")+
		                            				'</div>'+
		                            			'</span>'+
		                        			'</div>'+
		                    			'</div>'+
		            				'</li>';
				}else{
					disableHtml += 	'<li>'+
										'<div class="coupon-item c-disable coupon-item-new">'+
											'<div class="c-detail-disable">'+
												'<div class="c-msg">'+
													'<div class="c-top"></div>'+
													'<div class="item-c-content">'+
														'<div class="c-content">'+
															'<div class="c-price">'+
																'<em>￥'+(v.sellingprice||0)+'</em>'+
															'</div>'+
															'<div class="c-limit">'+
//																'<span>'+v.cptext+'</span>'+
																'<span class="custom_tip" data-tip="'+v.cptext+'" style="margin-left:5px;">'+v.cptext+'</span>'+
															'</div>'+
														'</div>'+
														'<div class="c-time c-time-dong">'+
//															'<span>有效期至2020-04-12</span>'+
															'<span>可用['+v.available+']</span>'+
															'<span style="margin-left:5px;">已用['+(v.quantity-v.available)+']</span>'+
			                                            '</div>'+
													'</div>'+
		                                        '</div>'+
		                                        '<div class="c-type">'+
//		                                        	'<span class="c-type-l">['+v.cpcode+']</span>'+
//			                                        '<span class="c-type-r" id="59430242451">[限品类]</span>'+
//			                                        '<span title="可与店铺东券叠加使用" style="margin-left:7px;">[可叠加]</span>'+
													$.i18n.propJsp("期限")+"["+v.edate+"]"+
		                                        '</div>'+
		                                    '</div>'+
		                                    '<div class="c-info">'+
		                                    	'<i class="c-info-qmark"></i>'+
		                                    	'<span>'+
		                                    		'<div class="virtual-desc" title="'+v.status_c+" "+v.remark+'">'+
//		                                    			'此券在2020-04-11 00:00:00后可使用'+
		                                    			v.status_c+" "+(v.remark||"")+
		                            				'</div>'+
		                            			'</span>'+
		                        			'</div>'+
		                    			'</div>'+
		            				'</li>';
				}
			});
			
			
			html = 	'<div class="coupon-enable">'+
            			'<ul style="width:'+param.width+'px;">'+
            				enableHtml+
            			'</ul>'+
        			'</div>'+
        			'<div class="coupon-disable">'+
            			'<ul style="width:'+param.width+'px;">'+
            				usedHtml+
            				disableHtml+
            			'</ul>'+
        			'</div>';
			el.html(html);
			el.find(".useCoupon").unbind("click").click(function(){
				var txt = $(this).find("span.json").text();
				var json = $.parseJSON(txt);
				param.useBack(json);
			})
			showTip();
		},
		initCoupon_gotHtml1 : function(ds){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var html = '';
			var enableHtml = '';
			var usedHtml = '';
			var disableHtml = '';
			console.log(JSON.stringify(ds));
			$(ds).each(function(k,v){
				var btnHtml = ''
					html += '<div class="item-li layoutRound">'+
								'<div class="item_main">'+
									'<div class="item_left fl">'+
										'<div class="item_price middle">'+
											'<div>'+
												'<em>'+(v.cptext||"")+'</em>'+
											'</div>'+
											'<div class="price_info">'+
												'<b class="fsLarge">'+
													(v.sellingprice||"")+'<span>元</span>'+//v.act_rate
												'</b>'+
											'</div>'+
										'</div>'+
									'</div>'+
									'<div class="item_cont fl">'+
										'<h3>'+(v.cptext||"")+'</h3>'+
										'<div class="act_name ellips">有效期:'+v.edate+'</div>'+
										'<div class="act_duration">状态:'+v.status_c+'(数量:'+v.available+')</div>'+//2020-06-17前可领取
										'<div class="remark_btn">'+
											'<div class="remark_text" title="'+(v.remark||"")+'">'+(v.remark||"")+'</div>'+//详细说明
										'</div>'+
									'</div>'+
									'<div class="item_right fl receive_promocode" data-index="0">'+
										'<i class="icon_received" style="margin-top:30px;"></i>'+
//										'<div data-getcode="'+v.getcode+'" data-couponid="'+v.couponid+'" data-mno="'+param.mno+'" class="btn" style="background-color: rgb(249, 115, 106) !important;color: #f5f5f5 !important;">去使用</div>'+
									'</div>'+
								'</div>'+
								'<div class="item_sub_list">'+
									'<div class="item_product"></div>'+
									'<div class="item_sub">'+
										'<ul class="item_tips">'+
											'<li>'+
												'<span class="tips_num">1</span>'+
												'满189元减12元'+
											'</li>'+
										'</ul>'+
										'<ul class="item_remark_tmpl">'+
											'<li>1、本优惠券仅限页面显示“已减”字样门票产品使用，订单金额满189元减12元。</li>'+
											'<li> 2、有效期内同一用户限领取/使用1次。</li>'+
											'<li> 3、此优惠券仅限在携程旅行客户端预订时使用该优惠券。</li>'+
											'<li>4、不可与其他优惠叠加使用。</li>'+
											'<li>5.如发现恶意盗刷优惠券等行为，携程有权收回该行为涉及的优惠券，并取消所购产品。</li>'+
										'</ul>'+
									'</div>'+
								'</div>'+
							'</div>';
			});
			el.html(html);
			
		},
		//获取可领券
		coupon_gotDefault : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			if(!(param.mno&&param.mno!=0)){
				return false;
			}
			param.wait();
			var arg = {"arg":{"arg":"全部"},"where":{"mname_c":param.mno}};
			jQuery.ajax({
				type :'POST',  
				url : $.HSAPI_AJAXURL+"/qms/mobile/procedure2",
				data : $.HSAPI_ASKJSONINFO( JSON.stringify(arg),'HoMemberCouponSearch','[{"page":1,"sord":"asc","rows":"全部","sidx":"cptext"}]',"listWithPage"),
				dataType : 'json',
				success : function(data) {
					param.goon();
					if(typeof data=="string"){
						data = $.parseJSON(data);
					}
					var ds = data.rows;
					t.initCoupon_gotHtml(ds);
				},  
				error : function(data) {  
					param.goon();
					jf_alert(data);
				}  
			});
		},
	}
	
})(jQuery);